package edu.hrbu.trade01.controller;

import edu.hrbu.trade01.entity.Menu;
import edu.hrbu.trade01.service.FirmService;
import edu.hrbu.trade01.service.MenuService;
import edu.hrbu.trade01.service.OperLogService;
import edu.hrbu.trade01.util.MenuUtil;
import edu.hrbu.trade01.util.R;
import edu.hrbu.trade01.vo.AnalysisVo;
import edu.hrbu.trade01.vo.MenuAnalysisVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐登宇
 */
@Slf4j
@RestController
@RequestMapping("/analysis")
public class AnalysisController {

    @Autowired
    private OperLogService operLogService;

    @Autowired
    private FirmService firmService;

    @Autowired
    private MenuService menuService;

    @PostMapping("/menu")
    public R menu(){
        List<Menu> list = menuService.list();
        ArrayList<Menu> menus = MenuUtil.buildTree(list, 1);
//        log.error("@@@@@@@:"+menus);
        ArrayList<MenuAnalysisVo> menuAnalysisVoList = new ArrayList<>();
        menus.forEach(i->{
            if (i.getChild().size()!=0){
                MenuAnalysisVo e = new MenuAnalysisVo();
                e.setName(i.getTitle());
                e.setValue(i.getChild().size());
                menuAnalysisVoList.add(e);
            }

        });
        return R.ok(menuAnalysisVoList);
    }

    @PostMapping("/firm")
    public R firm(){
        List<AnalysisVo> list = firmService.analysis();
        return R.ok(list);
    }

    @PostMapping("/log")
    public R log(){
        List<AnalysisVo> list = operLogService.analysis();
        return R.ok(list);
    }

}
